package com.ry.widget;

/**
 * Create by renxianzuo on 2020/12/2
 * Describe
 */

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;

/**
 *
 * 圆形带箭头进度条
 *
 * Create by renxianzuo on 2020/12/2
 * Describe
 */
public class MyCircleView extends androidx.appcompat.widget.AppCompatTextView {
    private float mProgressValue = 0.75f;
    private Paint mDefaultPaint = new Paint();
    private Paint mProgressPaint = new Paint();
    private Paint demo = new Paint();
    RectF rectf = new RectF();

    Paint paint = new Paint();

    ValueAnimator animator;

    float ratio = 0;

    public MyCircleView(Context context) {
        super(context);
        initPaint(context);
    }

    public MyCircleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint(context);
    }

    public MyCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint(context);
    }

    private void initPaint(Context context) {

        mDefaultPaint.setAntiAlias(true);
        mDefaultPaint.setStyle(Paint.Style.STROKE);
        mDefaultPaint.setColor(0x48FFFFFF);
//        mDefaultPaint.setColor(Color.BLUE);
        mDefaultPaint.setStrokeWidth(dipToPx(context,1f));

        mProgressPaint.setAntiAlias(true);
        mProgressPaint.setStyle(Paint.Style.STROKE);
        mProgressPaint.setColor(0xFFFFFFFF);
//        mProgressPaint.setColor(Color.RED);
        mProgressPaint.setStrokeWidth(dipToPx(context,1.37f));

        demo.setAntiAlias(true);
        demo.setStyle(Paint.Style.FILL);
        demo.setColor(0xFFFFFFFF);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
      /*  int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        //设置画得一个半径，然后比较长和宽，以最大的值来确定长方形的长宽，确定半径
        int min = Math.min(width, height);
        rectf.set(getPaddingLeft(), getPaddingTop(), min - getPaddingRight(), min - getPaddingBottom());

        float angle = 360 * mProgressValue;
        //绘制圆弧
        canvas.drawArc(rectf, -90, 360, false, mDefaultPaint);

        canvas.drawArc(rectf, -90, angle, false, mProgressPaint);

        angle = angle - 90;
        *//**
         * 圆点坐标：(center_x,center_y)
         * 半径：r1
         * 角度：a0
         *
         * 则圆上任一点为：（x1,y1）
         * x1   =   center_x   +   r1   *   cos(ao   *   3.14   /180   )
         * y1   =   center_y   +   r1   *   sin(ao   *   3.14   /180   )
         *
         * pointX = x + radius * Math.cos(Math.toRadians(angle))
         * pointY = y + radius * Math.sin(Math.toRadians(angle))
         *
         *//*

        int center_x = width / 2;
        int center_y = height / 2;

        int r1 = (int) (rectf.width() / 2);
        int x1 = (int) (center_x + r1 * Math.cos(Math.toRadians(angle)));
        int y1 = (int) (center_y + r1 * Math.sin(Math.toRadians(angle)));

        *//**
         * 计算第三条边长
         * cosC=(a^2+b^2-c^2)/2ab
         *//*

        int arrow_length = dipToPx(getContext(),4);
        int arrow_angle = 65;

        //计算半径
        double r2 =  Math.sqrt(Math.pow(arrow_length, 2) + Math.pow(r1, 2)- Math.cos(Math.toRadians(90+arrow_angle+4)) * (2 * arrow_length * r1));

        //rr 角度
        double r2_angle =  Math.acos((Math.pow(r1, 2) + Math.pow(r2, 2) - Math.pow(arrow_length, 2)) / (2 * r1 * r2));

        int x2 = (int) (center_x + r2 * Math.cos(Math.toRadians(angle-r2_angle*360)));
        int y2 = (int) (center_y + r2 * Math.sin(Math.toRadians(angle-r2_angle*360)));

        double r3 = Math.sqrt(Math.pow(arrow_length, 2) + Math.pow(r1, 2)- Math.cos(Math.toRadians(90-arrow_angle-8)) * (2 * arrow_length * r1));
        double r3_angle =  Math.acos((Math.pow(r1, 2) + Math.pow(r3, 2) - Math.pow(arrow_length, 2)) / (2 * r1 * r3));

        int x3 = (int) (center_x + r3 * Math.cos(Math.toRadians(angle-r3_angle*360)));
        int y3 = (int) (center_y + r3 * Math.sin(Math.toRadians(angle-r3_angle*360)));

//        canvas.drawText("angle = "+angle+" ，r2_angle="+r2_angle+" ，r2="+r2+",r3_angle="+r3_angle, 100, 20, demo);
//        canvas.drawCircle(center_x, center_y, 2, demo);
//        canvas.drawCircle(x1, y1, 2, demo);
//        canvas.drawCircle(x2, y2, 2, demo);
//        canvas.drawCircle(x3, y3, 2, demo);
//
//        canvas.drawCircle(center_x, center_y, (float)r2, mDefaultPaint);
//        canvas.drawCircle(center_x, center_y, (float)r3, mDefaultPaint);
//        canvas.drawLine(center_x, center_y, x2, y2, mDefaultPaint);
//        canvas.drawLine(center_x, center_y, x3, y3, mProgressPaint);
//
//        canvas.drawText("(" + center_x + "," + center_y + ")", 50, 50, demo);
//
//        canvas.drawText("(" + x1 + "," + y1 + ")", 50, 100, demo);
//
//        canvas.drawText("(" + x2 + "," + y2 + ")", 50, 150, demo);

//        canvas.drawText(mProgressValue+"", 100, 20, demo);

        if(mProgressValue > 0) {
            canvas.drawLine(x2, y2, x1, y1, mProgressPaint);

            canvas.drawLine(x3, y3, x1, y1, mProgressPaint);
        }
*/
        drawDemo2(canvas);
    }

    void drawDemo2(Canvas canvas){
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        //设置画得一个半径，然后比较长和宽，以最大的值来确定长方形的长宽，确定半径
        int min = Math.min(width, height);
        rectf.set(getPaddingLeft(), getPaddingTop(), min - getPaddingRight(), min - getPaddingBottom());

        float angle = 360 * mProgressValue;

//        if (animator != null && animator.isRunning()) {
//            float ratio = (Float)animator.getAnimatedValue();
//            Log.d("RXZ","ratio="+ratio);
            angle = angle * ratio;
            //绘制圆弧
            canvas.drawArc(rectf, -90, 360, false, mDefaultPaint);

            canvas.drawArc(rectf, -90, angle, false, mProgressPaint);

            angle = angle - 90;

            int center_x = width / 2;
            int center_y = height / 2;

            int r1 = (int) (rectf.width() / 2);
            int x1 = (int) (center_x + r1 * Math.cos(Math.toRadians(angle)));
            int y1 = (int) (center_y + r1 * Math.sin(Math.toRadians(angle)));

            /**
             * 计算第三条边长
             * cosC=(a^2+b^2-c^2)/2ab
             */

            int arrow_length = dipToPx(getContext(),4);
            int arrow_angle = 65;

            //计算半径
            double r2 =  Math.sqrt(Math.pow(arrow_length, 2) + Math.pow(r1, 2)- Math.cos(Math.toRadians(90+arrow_angle+4)) * (2 * arrow_length * r1));
            //rr 角度
            double r2_angle =  Math.acos((Math.pow(r1, 2) + Math.pow(r2, 2) - Math.pow(arrow_length, 2)) / (2 * r1 * r2));

            int x2 = (int) (center_x + r2 * Math.cos(Math.toRadians(angle-r2_angle*360)));
            int y2 = (int) (center_y + r2 * Math.sin(Math.toRadians(angle-r2_angle*360)));

            double r3 = Math.sqrt(Math.pow(arrow_length, 2) + Math.pow(r1, 2)- Math.cos(Math.toRadians(90-arrow_angle-8)) * (2 * arrow_length * r1));
            double r3_angle =  Math.acos((Math.pow(r1, 2) + Math.pow(r3, 2) - Math.pow(arrow_length, 2)) / (2 * r1 * r3));

            int x3 = (int) (center_x + r3 * Math.cos(Math.toRadians(angle-r3_angle*360)));
            int y3 = (int) (center_y + r3 * Math.sin(Math.toRadians(angle-r3_angle*360)));


            if(mProgressValue > 0) {
                canvas.drawLine(x2, y2, x1, y1, mProgressPaint);

                canvas.drawLine(x3, y3, x1, y1, mProgressPaint);
            }

//        }
    }

    void drawDemo(Canvas canvas){
        int radius = 100;
        //设置抗锯齿
        paint.setAntiAlias(true);
        paint.setColor(Color.RED);
        paint.setStrokeWidth(1);
        //设置显示风格为空心圆
        paint.setStyle(Paint.Style.STROKE);
        int pivotX = getWidth() / 2;
        int pivotY = getHeight() / 2;
        canvas.drawCircle(pivotX, pivotY,radius,paint);
        canvas.save();
        //旋转画布 , 如果旋转的的度数大的话,视觉上看着是旋转快的
//        canvas.rotate(90,pivotX,pivotY);

        //提供了一些api可以用来画线(画路径)
        Path path = new Path();
        //从哪开始画 从A开始画
        path.moveTo(pivotX+radius,pivotY);
        //从A点画一个直线到D点
        path.lineTo(pivotX + radius - 20, pivotY -20);
        //从D点画一个直线到B点
        path.lineTo(pivotX+radius,pivotY+20);
        //从B点画一个直线到C点
        path.lineTo(pivotX+radius+20,pivotY-20);
        //闭合  --  从C点画一个直线到A点
        path.close();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.BLUE);
        canvas.drawPath(path, paint);
    }

    public void setProgress(float progress) {
        this.mProgressValue = progress;
        startAnimator();
    }

    void startAnimator() {
        if(animator == null) {
            animator = ValueAnimator.ofFloat(0f, 1f);
            animator.setDuration(2000);
            animator.setInterpolator(new LinearInterpolator());
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    // 获取到动画每次该变得float值，赋值给xpoint
                    ratio = (Float) animation.getAnimatedValue();
                    // 通知view重绘
                    invalidate();
                }
            });
        }
        animator.start();
    }

    /**
     * dp转px
     *
     * @param context context
     * @param dpValue dp
     * @return px
     */
    private static int dipToPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
